package com.zlj.xstream;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.thoughtworks.xstream.XStream;

/**
 * 演示XStream
 */
public class XStreamTest {
	//返回JavaBean集合
	public List<Province> getProvinceList() {
		Province p = new Province();
		p.setName("北京");
		p.addCity(new City("东城区","dongchengqu"));
		p.addCity(new City("西城区","xichengqu"));
		
		Province p2 = new Province();
		p2.setName("延安");
		p2.addCity(new City("子丹","zidan"));
		p2.addCity(new City("子州","zizhou"));
		
		List<Province> provinceList = new ArrayList<Province>();
		provinceList.add(p);
		provinceList.add(p2);
		return provinceList;
	}
	/*
	<list>  ---> List类型显示list 
	  <com.zlj.xstream.Province>   ---> javaBean的类型为Province，它元素的名称为类的完整名
	    <name>北京</name>    ---> javaBean 的属性名
	    <cities>   ---> javaBean 的属性名
	      <com.zlj.xstream.City>    ---> 类名
	        <name>东城区</name>   ---> 属性名
	        <description>dongchengqu</description>   ---> 属性名
	      </com.zlj.xstream.City>
	      <com.zlj.xstream.City>
	        <name>西城区</name>
	        <description>xichengqu</description>
	      </com.zlj.xstream.City>
	    </cities>
	  </com.zlj.xstream.Province>
	  <com.zlj.xstream.Province>
	    <name>延安</name>
	    <cities>
	      <com.zlj.xstream.City>
	        <name>子丹</name>
	        <description>zidan</description>
	      </com.zlj.xstream.City>
	      <com.zlj.xstream.City>
	        <name>子州</name>
	        <description>zizhou</description>
	      </com.zlj.xstream.City>
	    </cities>
	  </com.zlj.xstream.Province>
	</list>
	 */
	@Test
	public void fun1() {
		/*
		 * 1、创建XStream对象
		 * 2、调用toXml()方法把集合转换为xml字符串
		 */
		XStream xstream = new XStream();
		String xml = xstream.toXML(getProvinceList());
		System.out.println(xml);
	}
	
	/*
	 * 设置别名(alias)：
	 * 默认List类型对应<list>元素，希望让List类型对应<china>元素
	 * 默认Province类型对应<com.zlj.xstream.Province>,希望让它对应<province>
	 * 默认City类型对应<com.zlj.xstream.City>,希望让它对应<city>元素
	 */
	/*
	 <china>
		  <province>
		    <name>北京</name>
		    <cities>
		      <city>
		        <name>东城区</name>
		        <description>dongchengqu</description>
		      </city>
		      <city>
		        <name>西城区</name>
		        <description>xichengqu</description>
		      </city>
		    </cities>
		  </province>
		  <province>
		    <name>延安</name>
		    <cities>
		      <city>
		        <name>子丹</name>
		        <description>zidan</description>
		      </city>
		      <city>
		        <name>子州</name>
		        <description>zizhou</description>
		      </city>
		    </cities>
		  </province>
		</china>
	 */
	@Test
	public void fun2() {
		XStream xstream = new XStream();
		/*
		 * 給指定的类型指定别名
		 */
		xstream.alias("china", List.class);   //给List类型指定别名为china
		xstream.alias("province", Province.class);   //给Province类型指定别名为province
		xstream.alias("city", City.class);    //给City类型指定别名为city
		
		
		String xml = xstream.toXML(getProvinceList());
		System.out.println(xml);
	}
	
	/*
	 * 默认javaBean的属性都会生成子元素，而现在希望生成元素的属性
	 */
	/*
	 <china>
		  <province name="北京">
		    <cities>
		      <city>
		        <name>东城区</name>
		        <description>dongchengqu</description>
		      </city>
		      <city>
		        <name>西城区</name>
		        <description>xichengqu</description>
		      </city>
		    </cities>
		  </province>
		  <province name="延安">
		    <cities>
		      <city>
		        <name>子丹</name>
		        <description>zidan</description>
		      </city>
		      <city>
		        <name>子州</name>
		        <description>zizhou</description>
		      </city>
		    </cities>
		  </province>
		</china>
	 */
	@Test
	public void fun3() {
		XStream xstream = new XStream();
		xstream.alias("china", List.class);   //给List类型指定别名为china
		xstream.alias("province", Province.class);   //给Province类型指定别名为province
		xstream.alias("city", City.class);    //给City类型指定别名为city
		
		/*
		 * 把Province类型的name属性，生成<province>元素的属性
		 */
		xstream.useAttributeFor(Province.class, "name");
		
		String xml = xstream.toXML(getProvinceList());
		System.out.println(xml);
	}
	
	/*
	 * 去除List类型的属性，只把list中的元素生成xml元素
	 */
	/*
	 <china>
		  <province name="北京">
		    <city>
		      <name>东城区</name>
		      <description>dongchengqu</description>
		    </city>
		    <city>
		      <name>西城区</name>
		      <description>xichengqu</description>
		    </city>
		  </province>
		  <province name="延安">
		    <city>
		      <name>子丹</name>
		      <description>zidan</description>
		    </city>
		    <city>
		      <name>子州</name>
		      <description>zizhou</description>
		    </city>
		  </province>
		</china>
	 */
	@Test
	public void fun4() {
		XStream xstream = new XStream();
		xstream.alias("china", List.class);   //给List类型指定别名为china
		xstream.alias("province", Province.class);   //给Province类型指定别名为province
		xstream.alias("city", City.class);    //给City类型指定别名为city
		xstream.useAttributeFor(Province.class, "name");  //把Province类型的name属性，生成<province>元素的属性
		
		/*
		 * 去除<cities>这样的Collection类型的属性
		 * 去除Province类的名为cities的List类型的属性
		 */
		xstream.addImplicitCollection(Province.class, "cities");
		String xml = xstream.toXML(getProvinceList());
		System.out.println(xml);
	}
	
	/*
	 * 去除不想要的JavaBean属性
	 * 就是让某些JavaBean属性不生成xml元素 
	 */
	/*
	  <china>
		  <province name="北京">
		    <city>
		      <name>东城区</name>
		    </city>
		    <city>
		      <name>西城区</name>
		    </city>
		  </province>
		  <province name="延安">
		    <city>
		      <name>子丹</name>
		    </city>
		    <city>
		      <name>子州</name>
		    </city>
		  </province>
		</china>
	 */
	@Test
	public void fun5() {
		XStream xstream = new XStream();
		xstream.alias("china", List.class);   //给List类型指定别名为china
		xstream.alias("province", Province.class);   //给Province类型指定别名为province
		xstream.alias("city", City.class);    //给City类型指定别名为city
		xstream.useAttributeFor(Province.class, "name");  //把Province类型的name属性，生成<province>元素的属性
		xstream.addImplicitCollection(Province.class, "cities"); //去除<cities>这样的Collection类型的属性 , 去除Province类的名为cities的List类型的属性
		
		/*
		 * 让City类的名为description属性，不生成对应的xml元素
		 */
		xstream.omitField(City.class, "description");
		
		String xml = xstream.toXML(getProvinceList());
		System.out.println(xml);
	}
}
